//
//  HSelectItemView.m
//  linktuIOS
//
//  Created by 俊松何 on 2017/3/22.
//  Copyright © 2017年 tianxie. All rights reserved.
//

#import "HSelectItemView.h"



@interface HSelectItemView()
@property (nonatomic,strong) NSMutableArray<UIButton *> *buttons;
@property (nonatomic,strong) UIView   *indicatorView;
@property (nonatomic,assign) NSInteger selectIndex;
@property (nonatomic,assign) NSInteger butTag;

@property(nonatomic,strong)  NSArray *titles;


@property(nonatomic,assign) CGFloat  selectColorR;
@property(nonatomic,assign) CGFloat  selectColorG;
@property(nonatomic,assign) CGFloat  selectColorB;


@property(nonatomic,assign) CGFloat  normalColorR;
@property(nonatomic,assign) CGFloat  normalColorG;
@property(nonatomic,assign) CGFloat  normalColorB;


@property(nonatomic,assign) CGFloat  rangColorR;
@property(nonatomic,assign) CGFloat  rangColorG;
@property(nonatomic,assign) CGFloat  rangColorB;

@end

@implementation HSelectItemView

-(void)layoutSubviews
{
    [super layoutSubviews];
     CGFloat padding = 30;
    
    CGFloat height = self.frame.size.height - 2;
    CGFloat width = (self.frame.size.width - padding *2)  / self.buttons.count;
    CGFloat y = 0;
    CGFloat x = 0;
    
   
    
    for (int i = 0; i < self.buttons.count; i ++) {
        x= width * i + padding;
        UIButton *but = self.buttons[i];
        but.frame = CGRectMake(x, y, width, height);
    }
    self.indicatorView.frame = CGRectMake(0,height, 80, 2);
    self.indicatorView.centerX = self.buttons[_selectIndex].centerX;
}

-(instancetype)initWithFrame:(CGRect)frame withtitles:(NSArray *)titles
{
      if(self = [super initWithFrame:frame])
      {
          _titles= titles;
          _butTag = 934;
          _selectColorR = 152.0/255.0;
          _selectColorG = 179.0/225.0;
          _selectColorB = 55.0/255.0;
          
          _normalColorR = 0.0;
          _normalColorG = 0.0;
          _normalColorB = 0.0;
          
          _rangColorR = _selectColorR - _normalColorR;
          _rangColorG = _selectColorG - _normalColorG;
          _rangColorB = _selectColorB - _normalColorB;
          
          _selectIndex = 0;
          
          _buttons = [NSMutableArray array];
          self.backgroundColor =[UIColor lightGrayColor];
          self.indicatorView = [UIView new];
          _indicatorView.backgroundColor = [UIColor colorWithRed:_selectColorR green:_selectColorG blue:_selectColorB alpha:1];
          [self addSubview:_indicatorView];
          [titles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
             
              NSString *title = obj;
              UIButton *but = [UIButton new];
              [but setTitle:title forState:UIControlStateNormal];
              [self addSubview:but];
              [but addTarget:self action:@selector(butClcik:) forControlEvents:UIControlEventTouchUpInside];
              but.tag = self.butTag + idx;
              but.titleLabel.font = [UIFont systemFontOfSize:16];
              if(idx == 0)
              {
               [but setTitleColor:[UIColor colorWithRed:_selectColorR green:_selectColorG blue:_selectColorB alpha:1] forState:UIControlStateNormal];
              }else
              {
                  [but setTitleColor:[UIColor colorWithRed:_normalColorR green:_normalColorG blue:_normalColorB alpha:1] forState:UIControlStateNormal];
              
              }
              [_buttons addObject:but];
          }];
          
          
      }

    return self;
}


-(void)setTitleWithProgress:(CGFloat)progress
                sourceIndex:(NSInteger)sourceIndex
                targetIndex:(NSInteger)targetIndex
{
    // 1.取出sourceLabel/targetLabel
    UIButton   *sourceButton = self.buttons[sourceIndex];
    UIButton   *targetButton = self.buttons[targetIndex];

    // 2.处理滑块的逻辑
    CGFloat moveTotalX = targetButton.frame.origin.x - sourceButton.frame.origin.x;
    CGFloat moveX = moveTotalX * progress;
    self.indicatorView.centerX = sourceButton.centerX + moveX;
    
    
    UIColor *sourceColor = [UIColor colorWithRed:_selectColorR - _rangColorR*progress  green:_selectColorG - _rangColorG*progress blue:_selectColorB - _rangColorB*progress alpha:1];
    
    UIColor *tagatColor =  [UIColor colorWithRed:_normalColorR + _rangColorR*progress  green:_normalColorG + _rangColorG*progress blue:_normalColorB + _rangColorB*progress alpha:1];

    [sourceButton setTitleColor:sourceColor forState:UIControlStateNormal];
    [targetButton setTitleColor:tagatColor forState:UIControlStateNormal];
    
    _selectIndex = targetIndex;
}




-(void)butClcik:(UIButton *)but
{
    
    UIButton *oldbut = self.buttons[_selectIndex];
    
    [oldbut setTitleColor:[UIColor colorWithRed:_normalColorR green:_normalColorG blue:_normalColorB alpha:1] forState:UIControlStateNormal];
    [but setTitleColor:[UIColor colorWithRed:_selectColorR green:_selectColorG blue:_selectColorB alpha:1] forState:UIControlStateNormal];
    
    _selectIndex = but.tag - self.butTag;
    [UIView animateWithDuration:0.5 animations:^{
        self.indicatorView.centerX =  but.centerX;
    }];
    if(self.delegate && [self.delegate respondsToSelector:@selector(selectitemview:selectIndex:)])
    {
        [self.delegate selectitemview:self selectIndex:but.tag - self.butTag];
    }
    
}
@end
